package com.hxd.otc.module.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.common.enums.IdCardStatusEnum;
import com.hxd.otc.dialog.ForbiddenBankCardDialog;
import com.hxd.otc.module.my.adapter.BankCardAdapter;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.bean.UserStatusBean;
import com.hxd.otc.module.my.contract.BankCardContract;
import com.hxd.otc.module.my.presenter.BankCardPresenter;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.TitleBar;

import java.util.List;

import butterknife.BindView;

/**
 * Created by lzy on 2018/6/7 0007.
 * description ${收款方式界面，银行卡列表}
 */
public class BankCardActivity extends BaseRecyclerActivity<BankCardPresenter, BankCardAdapter, BankCardBean> implements BankCardContract.View, BankCardAdapter.OnDeleteBankListener {
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;

    public static final int REQUEST_CODE_ADD_CARD = 10001;
    private ForbiddenBankCardDialog<BankCardBean> mBankCardBeanForbiddenBankCardDialog;
    private String mReason;
    private ForbiddenBankCardDialog<String> mRealNameAuthFailTipDialog;
    private ForbiddenBankCardDialog<String> mTipSetValidTypeDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bank_card;
    }

    @Override
    protected BankCardPresenter createPresenter() {
        return new BankCardPresenter(this);
    }

    @Override
    public BankCardAdapter createAdapter() {
        BankCardAdapter bankCardAdapter = new BankCardAdapter(this);
        bankCardAdapter.setOnDeleteBankListener(this);
        return bankCardAdapter;
    }

    @Override
    protected void initView() {
        super.initView();
        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {
            }

            @Override
            public void onRight(View v) {
                skipAddBankCardUI();
            }
        });
        mPresenter.findCardStatus();
    }

    /**
     * 跳转到添加界面
     */
    private void skipAddBankCardUI() {

        if (!UserHelper.isSetExValidType()) {
            mTipSetValidTypeDialog = new ForbiddenBankCardDialog<String>(BankCardActivity.this)
                    .setMessage(getString(R.string.your_are_not_set_valid_type_mode_is_go_to_setting))
                    .setOnConfirmClickListener(new ForbiddenBankCardDialog.OnConfirmClickListener<String>() {
                        @Override
                        public void onConfirm(String data) {
                            $startActivity(SecurityCenterActivity.class);
                        }
                    });
            mTipSetValidTypeDialog.show();
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putBoolean(SelectBankTypeActivity.EXTRA_HAS_WEIXIN, hasBankType(BankType.WEXIN));
        bundle.putBoolean(SelectBankTypeActivity.EXTRA_HAS_ALIPAY, hasBankType(BankType.ALIPAY));
        $startActivityForResult(SelectBankTypeActivity.class, bundle, REQUEST_CODE_ADD_CARD);
    }

    public boolean hasBankType(BankType bankType) {
        if (bankType == null) return false;
        List<BankCardBean> bankCardBeans = mAdapter.getItems();
        if (bankCardBeans.size() == 0) return false;
        for (BankCardBean bankCardBean : bankCardBeans) {
            if (bankType.getType() == bankCardBean.getType()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);
    }

    private void showForbiddenBankCarDialog(final int position) {
        final BankCardBean item = mAdapter.getItem(position);
        if (mBankCardBeanForbiddenBankCardDialog == null) {
            mBankCardBeanForbiddenBankCardDialog = new ForbiddenBankCardDialog<>(this);
        }
        mBankCardBeanForbiddenBankCardDialog.dismiss();
        mBankCardBeanForbiddenBankCardDialog
                .setMessage(getString(R.string.my_forbidden_bankcar_message))
                .setData(item)
                .setOnConfirmClickListener(new ForbiddenBankCardDialog.OnConfirmClickListener<BankCardBean>() {
                    @Override
                    public void onConfirm(BankCardBean data) {
                        mPresenter.onDeleteTradeMode(position, item);
                    }
                }).show();

    }

    @Override
    public void onDelete(int position, BankCardBean item) {
        showForbiddenBankCarDialog(position);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDeleteTradeModeSuccess(int position, BankCardBean item) {
        showToast(getString(R.string.delete_succeed));
        List<BankCardBean> items = mAdapter.getItems();
        for (int i = 0; i < items.size(); i++) {
            BankCardBean bankCardBean = items.get(i);
            if (bankCardBean.getId().equals(item.getId())) {
                items.remove(bankCardBean);
                mAdapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void findCardStatusSuccess(UserStatusBean userStatusBean) {

        if (userStatusBean == null) return;

        UserHelper.updateCardStatus(IdCardStatusEnum.getIDCardStatusEnum(userStatusBean.getCardStatus()));

        mReason = userStatusBean.getReason();
        if (TextUtils.isEmpty(mReason)) {
            mReason = getString(R.string.unknown);
        }
    }

    @Override
    public void findCardStatusFail(String errMessage) {
        mReason = getString(R.string.unknown);
    }

    @Override
    public boolean isEnableLoadMore() {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD_CARD && resultCode == RESULT_OK) {
            mPresenter.onRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.dismissDialog(mRealNameAuthFailTipDialog);
        UIUtils.dismissDialog(mBankCardBeanForbiddenBankCardDialog);
        UIUtils.dismissDialog(mTipSetValidTypeDialog);
    }
}

